package cn.gok.homework;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Homework {
    public static void main(String[] args) {
        treeMap();
        arrayList();
    }

    public static void treeMap(){
        TreeMap<Book, String> map = new TreeMap<Book, String>(new MyComparator1());
        map.put(new Book(1,"西游记",140),"xxx");
        map.put(new Book(2,"水浒传",171),"xxx");
        map.put(new Book(3,"三国演义",93),"xxx");
        map.put(new Book(4,"奥特曼历险记",99),"xxx");
        map.put(new Book(5,"猴王争霸赛",151),"xxx");
        for (Book i : map.keySet()){
            System.out.println(i);
        }
        System.out.println();
        for (Map.Entry<Book, String> i : map.entrySet()){
            System.out.println(i.toString());
        }
        System.out.println();
    }
    public static void arrayList(){
        ArrayList<Book> list = new ArrayList<>();
        list.add(new Book(1,"西游记",140));
        list.add(new Book(2,"水浒传",171));
        list.add(new Book(3,"三国演义",93));
        list.add(new Book(4,"奥特曼历险记",99));
        list.add(new Book(5,"猴王争霸赛",151));
        Stream<Book> stream = list.stream();
        stream.filter(book -> {
            return book.getPrice()>150;
        }).sorted(new MyComparator2()).forEach(System.out::println);
    }
}

class Book{
    private int bid;
    private String bname;
    float price;

    public int getBid() {
        return bid;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bid=" + bid +
                ", bname='" + bname + '\'' +
                ", price=" + price +
                '}';
    }

    public Book(int bid, String bname, float price) {
        this.bid = bid;
        this.bname = bname;
        this.price = price;
    }
}

class MyComparator1 implements Comparator{

    @Override
    public int compare(Object o1, Object o2) {
        Book book1 = (Book) o1;
        Book book2 = (Book) o2;
        if (book1.getPrice() > book2.getPrice()){
            return 1;
        }else {
            return -1;
        }
    }
}

class MyComparator2 implements Comparator{

    @Override
    public int compare(Object o1, Object o2) {
        Book book1 = (Book) o1;
        Book book2 = (Book) o2;
        if (book1.getBid() > book2.getBid()){
            return 1;
        }else {
            return -1;
        }
    }
}